package AAA.web;


import AAA.exceptions.MoneyNotEnoughException;
import AAA.exceptions.TransferException;
import AAA.service.impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/transfer")
public class account extends HttpServlet {
    //为了让这个对象在其他方法中也可以使用，声明为实例变量
    private AccountServiceImpl accountService = new AccountServiceImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fromactno = req.getParameter("fromactno");
        String toactno = req.getParameter("toactno");
        String money1 = req.getParameter("money");
        double moeny = Double.parseDouble(money1);

        //调用servlet的转账方法完成转账。调业务层
        try {
            accountService.transfer(fromactno,toactno,moeny);
            //view视图层展示结果
            resp.sendRedirect(req.getContextPath()+"/success.html");
        } catch (MoneyNotEnoughException e) {
            throw new RuntimeException(req.getContextPath()+"/error1");
        } catch (TransferException e) {
            throw new RuntimeException(req.getContextPath()+"/error2");
        }







    }
}
